<template>
  <div class="login-page">
<van-nav-bar title="账号登录" />
    <!-- vant里的Form和Field  -->
     <!-- 一旦form表单提交了，就会触发submit，可以在submit事件中
         根据拿到的表单提交信息，发送axios请求
     -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label=""
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{3,}/, message: '用户名至少为3位数' },
        ]"
      />
      <!-- \w 字母数字_   \d 数字0-9 -->
      <!-- rules vant包里 匹配规则 -->
      <van-field
        v-model="password"
        type="password"
        name="password"
        label=""
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{3,}/, message: '用户名至少为3位数' },
        ]"
      />
      <!-- rules vant包里 匹配规则 -->

      <div style="margin: 16px ">
        <van-button round block type="info" native-type="submit"
          >登录</van-button>
      </div>
      <!-- 跳转到注册账号 -->
     <div class="one">
       <router-link class="link" to="/register">还没有账号,去注册</router-link>
     </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
import request from '@/utils/request'
export default {
  name: 'login-page',
  async created () {
    const res = await request.post('/user/login', {
      username: '',
      password: ''
    })
    console.log(res)
    setToken()
  },
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log(value)
      const { body } = await login(values)
      setToken(body.token)
      console.log(body.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    }
  }
}
</script>
<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
.one {
margin-top: 20px;
margin-right: 100px;
}
</style>
